package com.lijiajian.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijiajian.base.BaseService;
import com.lijiajian.dao.CustomerLossMapper;
import com.lijiajian.dao.CustomerReprieveMapper;
import com.lijiajian.pojo.CustomerOrder;
import com.lijiajian.pojo.CustomerReprieve;
import com.lijiajian.query.CustomerOrderQuery;
import com.lijiajian.query.CustomerReprieveQuery;
import com.lijiajian.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CustomerReprieveService extends BaseService<CustomerReprieve,Integer> {
    @Resource
    private CustomerReprieveMapper customerReprieveMapper;
    @Resource
    private CustomerLossMapper customerLossMapper;

    /**
     * 多条件查询暂缓
     * @param
     * @return
     */
    public Map<String,Object> queryCustomerReprieve(CustomerReprieveQuery customerReprieveQuery){
        Map<String,Object> map=new HashMap<>();
        //开启分页
        PageHelper.startPage(customerReprieveQuery.getPage(),customerReprieveQuery.getLimit());
        PageInfo<CustomerReprieve> pageInfo=new PageInfo<>(customerReprieveMapper.selectByParams(customerReprieveQuery));
        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());

        return map;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addCustomerReprieve(CustomerReprieve customerReprieve){
        AssertUtil.isTrue(null==customerReprieve.getLossId()||null==customerLossMapper.selectByPrimaryKey(customerReprieve.getLossId()),"流失客户记录不存在");

        AssertUtil.isTrue(StringUtils.isBlank(customerReprieve.getMeasure()),"暂缓措施内容不能为空");

        customerReprieve.setCreateDate(new Date());
        customerReprieve.setIsValid(1);
        customerReprieve.setUpdateDate(new Date());

        AssertUtil.isTrue(customerReprieveMapper.insertSelective(customerReprieve)!=1,"添加失败");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateCustomerReprieve(CustomerReprieve customerReprieve){
        AssertUtil.isTrue(null==customerReprieve.getId()||null==customerReprieveMapper.selectByPrimaryKey(customerReprieve.getId()),"待修改记录不存在");
        AssertUtil.isTrue(null==customerReprieve.getLossId()||null==customerLossMapper.selectByPrimaryKey(customerReprieve.getLossId()),"流失客户记录不存在");

        AssertUtil.isTrue(StringUtils.isBlank(customerReprieve.getMeasure()),"暂缓措施内容不能为空");

        customerReprieve.setUpdateDate(new Date());

        AssertUtil.isTrue(customerReprieveMapper.updateByPrimaryKeySelective(customerReprieve)!=1,"修改失败");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteCustomerReprieve(Integer[] ids){
        AssertUtil.isTrue(null==ids||ids.length<1,"待删除记录不存在");

        AssertUtil.isTrue(customerReprieveMapper.deleteBatch(ids)!=ids.length,"删除失败");
    }
}
